package com.sunwayworld.basemodule.samplemanage.samplehandle.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.samplemanage.sample.bean.LimsSampleBean;
import com.sunwayworld.basemodule.samplemanage.sample.service.LimsSampleService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.samplemanage.samplehandle.dao.LimsSampleHandleRelationDao;
import com.sunwayworld.basemodule.samplemanage.samplehandle.bean.LimsSampleHandleRelationBean;
import com.sunwayworld.basemodule.samplemanage.samplehandle.service.LimsSampleHandleRelationService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsSampleHandleRelationServiceImpl implements LimsSampleHandleRelationService {

    @Autowired
    private LimsSampleHandleRelationDao limsSampleHandleRelationDao;

    @Autowired
    @Lazy
    private LimsSampleService sampleService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsSampleHandleRelationDao getDao() {
        return limsSampleHandleRelationDao;
    }

    @Override
    @Transactional
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsSampleHandleRelationBean limsSampleHandleRelation = jsonWrapper.parseUnique(LimsSampleHandleRelationBean.class);
        limsSampleHandleRelation.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsSampleHandleRelation);
        return limsSampleHandleRelation.getId();
    }

    @Override
    @Transactional
    public Long scanCodeCheck(RestJsonWrapperBean wrapper) {
        List<JSONObject> body = wrapper.getBodyList().get(0).stream().map(JSON::parseObject).collect(Collectors.toList());
        if (!body.isEmpty()) {
            JSONObject item = body.get(0);
            Long sampleHandleId = item.getLong("sampleHandleId");
            String sampleCode = item.getString("sampleCode");
            List<LimsSampleBean> beans = sampleService.selectListByFilter(SearchFilter.instance()
                    .match("sampleHandleId", sampleHandleId).filter(MatchPattern.EQ)
                    .match("sampleCode", sampleCode).filter(MatchPattern.EQ));
            if (!beans.isEmpty()) {
                LimsSampleBean bean = beans.get(0);
                LimsSampleHandleRelationBean dataBean = selectFirstByFilter(SearchFilter.instance()
                        .match("handleId", sampleHandleId).filter(MatchPattern.EQ)
                        .match("sampleId", bean.getId()).filter(MatchPattern.EQ));
                if (dataBean != null) {
                    if (dataBean.getIsScanCodeCheckOver() != null && "true".equals(dataBean.getIsScanCodeCheckOver())) {
                        return 1L;
                    } else {
                        dataBean.setIsScanCodeCheckOver("true");
                        updateIfChanged(dataBean);
                        return dataBean.getSampleId();
                    }
                }
            }
        }
        return 2L;
    }
}
